﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Admin_Student_Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //检查用户权限
        if (Session["Uid"] == null)
        {
            //无权限返回登录页
            Response.Redirect("AD_Login.aspx");
            return;
        }
        //情况消息框
        lblMessage.Text = "";
        
        if (!IsPostBack)
        {
            Bind_ddlClass();
            //提取URL传参
            string ID = Request.QueryString["ID"].ToString();
            if (ID == "")
            {//如果参数为空
                lblTitle.Text = "添加学生简介";
                btnCancel.Text = "清空";
            }
            else
            {//如果参数不为空
                lblTitle.Text = "编辑学生简介";
                Bind_Txt(ID);
            }
        }
    }
    private void Bind_ddlClass()
    {
        //定义sql语句
        string sql = "select * from Class_Info";
        //取得数据
        SqlDataReader DR = DataAccess.GetReader(sql, null);
        ddlClass.DataSource = DR;
        ddlClass.DataTextField = "className";
        ddlClass.DataValueField = "classID";
        ddlClass.DataBind();
        DR.Close();
    }
    private void Bind_Txt(string ID)
    {
        //定义sql语句
        string sql = "select * from Student_Info where stuID=@stuID";
        //定义参数
        SqlParameter[] para = { new SqlParameter("@stuID", SqlDbType.Int) };
        //参数赋值
        para[0].Value = ID;
        //取得数据
        SqlDataReader DR = DataAccess.GetReader(sql, para);
        if (DR.HasRows)
        {//如果有数据
            while (DR.Read())
            {//显示数据
                ViewState["ID"] = ID;
                txtStu.Text = DR["stuName"].ToString();
                lblID.Text = ID;
                if (DR["stuSex"].ToString() == "男")
                {
                    rbnboy.Checked = true;
                    rbngirl.Checked = false;
                }
                else
                {
                    rbnboy.Checked = false;
                    rbngirl.Checked = true;
                }
                ddlClass.SelectedItem.Text = DR["stuclass"].ToString();
                txtContent.Text = DR["stuInfo"].ToString();
            }
        }
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        string Sex = "";
        if (this.rbnboy.Checked)
            Sex = "男";
        else
            Sex = "女";
        if (lblTitle.Text.Trim() == "添加学生简介")
        {//如果是添加班级简介
            //定义sql语句
            string sql = "insert into Student_Info(stuName,stuSex,stuClass,stuInfo) values(@stuName,@stuSex,@stuClass,@stuInfo)";
            //定义参数
            SqlParameter[] para = {
                    new SqlParameter("@stuName", SqlDbType.NVarChar,8),
                     new SqlParameter("@stuSex", SqlDbType.NChar,1),
                      new SqlParameter("@stuClass", SqlDbType.NVarChar,20),
                    new SqlParameter("@stuInfo", SqlDbType.NText)};
            //给参数赋值
            para[0].Value = txtStu.Text.Trim();
            para[1].Value = Sex;
           para[2].Value = this.ddlClass.SelectedItem.ToString();
            para[3].Value = txtContent.Text.Trim();

            if (DataAccess.GetCount(sql, para) > 0)
            {//如果添加成功
                //显示添加成功提示并转到管理页
                Response.Write("<script>alert('添加信息成功！');location.href='Student_Manage.aspx';</script>");
            }
            else
            {//如果添加失败
                lblMessage.Text = "添加信息失败";
            }
        }
        else if (lblTitle.Text.Trim() == "编辑学生简介")
        {
            //定义sql语句
            string sql = "update Student_Info set stuName=@stuName,stuSex=@stuSex,stuClass=@stuClass,stuInfo=@stuInfo where stuID=@stuID";
            //定义参数
            SqlParameter[] para = {
                    new SqlParameter("@stuName", SqlDbType.NVarChar,8),
                    new SqlParameter("@stuSex", SqlDbType.NChar,1),
                    new SqlParameter("@stuClass", SqlDbType.NVarChar,20),
                    new SqlParameter("@stuInfo", SqlDbType.NText),
                     new SqlParameter("@stuID", SqlDbType.Int)};
            //给参数赋值
            para[0].Value = txtStu.Text.Trim();
            para[1].Value = Sex;
           para[2].Value = ddlClass.SelectedItem.Text.Trim();
            para[3].Value = txtContent.Text.Trim();
            para[4].Value = lblID.Text.Trim();
            if (DataAccess.GetCount(sql, para) > 0)
            {//如果修改成功
                Response.Write("<script>alert('修改信息成功！');location.href='Student_Manage.aspx';</script>");
            }
            else
            {//如果修改失败
                lblMessage.Text = "修改信息失败";
            }
        }
    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (lblTitle.Text.Trim() == "添加毕业生简介")
        {
            txtStu.Text = "";
            rbnboy.Checked = false;
            rbngirl.Checked = false;
            ddlClass.SelectedItem.Text = "";
             txtContent.Text = "";
        }
        else if (lblTitle.Text.Trim() == "编辑学生简介")
        {
            //转到新管理简介管理界面
            Response.Redirect("Student_Manage.aspx");
        }
    }
   
}